package com.zyy.smartcity1129.me;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.FeedbackInfo;
import com.zyy.smartcity1129.data.MyFeedbackInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: MyFeedbackFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class MyFeedbackFragment extends BaseFragment {
    private RecyclerView rvMyFeedback;
    List<MyFeedbackInfo.RowsBean> feedbackList = new ArrayList<>();
    RvAd<MyFeedbackInfo.RowsBean> rvAd;

    @Override
    protected int layId() {
        return R.layout.f_myfeedback;
    }

    @Override
    protected String title() {
        right.setVisibility(View.VISIBLE);
        right.setText("新增");
        return "意见反馈";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(1);
        xcUtli.then(() -> {
            feedbackList = Http.getInstance().get("/prod-api/api/common/feedback/list", null, MyFeedbackInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (a.get() == 1) {
                initAdapter(feedbackList);
                rvMyFeedback.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMyFeedback.setAdapter(rvAd);
            }
        });
        right.setOnClickListener(view -> {
            PpUtil ppUtil = new PpUtil();
            View v = ppUtil.getP(requireContext(), R.layout.p_feedback_add);
            FrameLayout pF1 = (FrameLayout) v.findViewById(R.id.p_f1);
            EditText editPContext = (EditText) v.findViewById(R.id.editPContext);
            EditText editPTitle = (EditText) v.findViewById(R.id.editPTitle);
            Button btnPSubmit = (Button) v.findViewById(R.id.btnPSubmit);
            FrameLayout pF2 = (FrameLayout) v.findViewById(R.id.p_f2);
            pF1.setOnClickListener(v2 -> {
                ppUtil.closeP();
            });
            pF2.setOnClickListener(v2 -> {
                ppUtil.closeP();
            });
            btnPSubmit.setOnClickListener(v2 -> {
                String title = editPTitle.getText().toString().trim();
                String context = editPContext.getText().toString().trim();
                th(() ->{
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("title", title);
                    jsonObject.addProperty("content", context);
                    String result = Http.getInstance().get("/prod-api/api/common/feedback",
                            RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                    Log.i("TAG", result);
                    ui(() ->{
                        Toast.makeText(getContext(), "已提交，待审核", Toast.LENGTH_SHORT).show();
                    });
                });
                ppUtil.closeP();
            });
        });
    }

    private void initAdapter(List<MyFeedbackInfo.RowsBean> feedbackList) {
        rvAd = new RvAd<MyFeedbackInfo.RowsBean>(feedbackList, R.layout.rv_feedback) {
            private TextView tvRvFeedBack1, tvRvFeedBack2, tvRvFeedBack3;
            private void initView(View v) {
                tvRvFeedBack1 = (TextView) v.findViewById(R.id.tvRvFeedBack1);
                tvRvFeedBack2 = (TextView) v.findViewById(R.id.tvRvFeedBack2);
                tvRvFeedBack3 = (TextView) v.findViewById(R.id.tvRvFeedBack3);
            }
            @Override
            protected void initR(View vi, MyFeedbackInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tvRvFeedBack1.setText(rowsBean.getTitle());
                tvRvFeedBack2.setText("提交时间：" + rowsBean.getCreateTime());
                tvRvFeedBack3.setText(rowsBean.getContent());
            }
        };
    }

    private void initView(View iv) {
        rvMyFeedback = (RecyclerView) iv.findViewById(R.id.rvMyFeedback);
    }
}
